<template>
  <el-dialog title="新建员工" :visible.sync="formVisible">
    <el-form ref="dataForm" :rules="formRules" :model="form"
             label-position="left" size="small"
             label-width="100px" style="width: 400px; margin-left:50px;">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="出生日期" prop="birth">
        <el-date-picker v-model="form.birth"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="form.note" type="textarea" :rows="2"></el-input>
      </el-form-item>
      <el-form-item label="职位ID" prop="p_id">
        <el-select v-model="form.p_id"
                   style="width:100%;" placeholder="请选择"
                   filterable clearable>
          <el-option v-for="item in options.position"
                     :key="item.key"
                     :label="item.value"
                     :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="formVisible = false">
        取消
      </el-button>
      <el-button type="primary"
                 @click="doCreate()">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import employeeApi from '@/api/employee/employee'
import positionApi from '@/api/position/position'

function initFormBean() {
  const formBean = {
    name: null,
    birth: null,
    note: null,
    p_id: null
  }
  return formBean
}

export default {
  name: 'EmployeeAdd',
  data() {
    return {
      options: {
        position: []
      },
      form: initFormBean(),
      formVisible: false,
      formRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { max: 32, message: '长度不能超过32个字符', trigger: 'blur' }
        ],
        birth: [
          { required: true, message: '请输入出生日期', trigger: 'change' }
        ],
        note: [
          { max: 255, message: '长度不能超过255个字符', trigger: 'blur' }
        ],
        p_id: [
          { required: true, message: '请输入职位ID', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    /**
     * 重置表单
     */
    resetForm() {
      this.form = initFormBean()
    },
    /**
     * 打开新建表单
     */
    handleCreate() {
      this.resetForm()
      positionApi.findOptions().then(data => { this.options.position = data })
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    /**
     * 执行新建操作
     */
    doCreate() {
      this.$refs['dataForm'].validate()
        .then(() => employeeApi.create(this.form))
        .then(data => {
          this.formVisible = false
          this.$common.showMsg('success', '创建成功')
          this.$emit('created', data)
        })
    }
  }
}
</script>
